﻿using EPlantApp.BusinessInterface;
using EPlantApp.Common.Result;
using SqlSugar;
using System.Linq.Expressions;

namespace EPlantApp.BusinessService
{
    public abstract class BaseService : IBaseService
    {
        private readonly ISqlSugarClient _client;
        public BaseService(ISqlSugarClient client)
        {
            _client = client;
        }

        #region Query
        public T Find<T>(int id) where T : class
        {
            return _client.Queryable<T>().InSingle(id);
        }

        public async Task<T> FindAsync<T>(int id) where T : class
        {
            return await _client.Queryable<T>().InSingleAsync(id);
        }

        public ISugarQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
        {
            return _client.Queryable<T>().Where(funcWhere);
        }

        public PagingData<T> QueryPage<T>(Expression<Func<T, bool>> funcWhere, int pageSize, int pageIndex, Expression<Func<T, object>> funcOrderby, bool isAsc = true) where T : class
        {
            var list = _client.Queryable<T>().SplitTable();
            if (funcWhere != null)
            {
                list = list.Where(funcWhere);
            }
            list = list.OrderByIF(true, funcOrderby, isAsc ? OrderByType.Asc : OrderByType.Desc);
            PagingData<T> result = new PagingData<T>()
            {
                DataList = list.ToPageList(pageIndex, pageSize),
                PageIndex = pageIndex,
                PageSize = pageSize,
                RecordCount = list.Count()
            };
            return result;
        }
        #endregion

        #region Insert
        public T Insert<T>(T t) where T : class, new()
        {
            return _client.Insertable(t).ExecuteReturnEntity();
        }

        public async Task<T> InsertAsync<T>(T t) where T : class, new()
        {
            return await _client.Insertable(t).ExecuteReturnEntityAsync();
        }

        public async Task<bool> InsertList<T>(List<T> tList) where T : class, new()
        {
            return await _client.Insertable(tList.ToList()).ExecuteCommandIdentityIntoEntityAsync();
        }
        #endregion

        #region Update
        public void Update<T>(List<T> tList) where T : class, new()
        {
            _client.Updateable(tList).ExecuteCommand();
        }

        public async Task<bool> UpdateAsync<T>(T t) where T : class, new()
        {
            if (t == null) throw new Exception("t is null");
            return await _client.Updateable(t).ExecuteCommandHasChangeAsync();
        }
        #endregion

        #region Delete
        public bool Delete<T>(object pId) where T : class, new()
        {
            T t = _client.Queryable<T>().InSingle(pId);
            return _client.Deleteable(t).ExecuteCommand() > 0;
        }

        public void Delete<T>(T t) where T : class, new()
        {
            _client.Deleteable(t).ExecuteCommand();
        }

        public void Delete<T>(List<T> tList) where T : class
        {
            _client.Deleteable(tList).ExecuteCommand();
        }
        #endregion

        #region Other
        public ISugarQueryable<T> ExcuteQuery<T>(string sql) where T : class, new()
        {
            return _client.SqlQueryable<T>(sql);
        }

        public void Dispose()
        {
            if(_client != null )
            {
                _client.Dispose();
            }
        }
        #endregion









    }
}
